package net.easyjava.admin.controller;

import net.easyjava.admin.constants.Webconstants;
import net.easyjava.base.bean.UserBean;
import net.easyjava.base.manager.UserManager;
import net.easyjava.tools.model.ModelBean;
import net.easyjava.tools.model.ModelConstants;
import net.easyjava.tools.page.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * Created by wenxiaoning on 15/12/18.
 */
@Controller
@RequestMapping("/admin/user")
public class UserController extends BaseController {
    private  Logger logger = LoggerFactory.getLogger(this.getClass().getName());
    @Resource
    private UserManager userManager;


    /**
     * 同一个浏览器用户第一次访问页面的时候返回一个随机用户
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/{type}.html")
    @ResponseBody
    public ModelAndView getUserByType(
            @PathVariable String type,
            //注册用参数
            @RequestParam(required = false,defaultValue = "") String name,
            @RequestParam(required = false,defaultValue = "") String password,

            HttpServletRequest request, HttpServletResponse response) {
        String message = ModelConstants.MESSAGE_ERROR;
        try {

            if(Webconstants.USER_LOGIN.equals(type)){

                UserBean userBean = new UserBean();
                userBean.setUserName(name);
                userBean.setPassword(password);
                return new ModelAndView("index.html","user",userBean);
            }else {
                return null;
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e.fillInStackTrace());
            return null;
        }

    }


    @RequestMapping("/{type}")
    @ResponseBody
    public ModelBean json(
            @PathVariable String type,
            //注册用参数
            @RequestParam(required = false,defaultValue = "") String name,
            @RequestParam(required = false,defaultValue = "") String password,

            HttpServletRequest request, HttpServletResponse response) {
        ModelBean modelBean = null;
        String message = ModelConstants.MESSAGE_ERROR;
        try {


        }catch (Exception e){
            logger.error(e.getMessage(),e.fillInStackTrace());
            modelBean = new ModelBean(message);
        }

        return new ModelBean(new Page<>(Arrays.asList("1,2"),2,1));

    }


}
